package com.financing.wap.web.controller.api;

import java.util.Collections;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.financing.wap.common.Pager;
import com.financing.wap.common.ResultMap;
import com.financing.wap.dto.FpShopInformationDTO;
import com.financing.wap.dto.FpShopInformationDzDTO;
import com.financing.wap.service.IFpShopInformationDzService;
import com.financing.wap.service.IFpShopInformationService;
import com.financing.wap.util.DTOdateSetUtil;
import com.financing.wap.web.controller.BaseController;

@Controller("fpShopInformationDzController")
@RequestMapping("/api/fpShopInformationDz")
public class FpShopInformationDzController extends BaseController  { 

	private Logger logger = Logger.getLogger(getClass());

	@Autowired
	private IFpShopInformationDzService fpShopInformationDzService;
	@Autowired
	private IFpShopInformationService fpShopInformationService;

	@RequestMapping("/listFpShopInformationDz")
	@ResponseBody
	public ResultMap list(HttpServletRequest request, FpShopInformationDzDTO fpShopInformationDz,Pager pager){
		ResultMap result = new ResultMap();

		try{
			logger.info("query fpShopInformationDz list begin .......");

			//			fpShopInformationDz.setBegin(pager.getStartIndex());
			//			fpShopInformationDz.setPageSize(pager.getPageSize());

			int count = fpShopInformationDzService.queryListCount(fpShopInformationDz);

			pager.setTotalCount(count);

			List<FpShopInformationDzDTO> fpShopInformationDzList = null;

			if(count > 0){
				fpShopInformationDzList = fpShopInformationDzService.queryList(fpShopInformationDz);
			}else{
				fpShopInformationDzList = Collections.emptyList();
			}
			result.setAttribute("pager", pager);
			result.setAttribute("list", fpShopInformationDzList);
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);

			logger.info("query fpShopInformationDz list end .......");
		}catch(Exception e){
			log.error("query fpShopInformationDz error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}

		return result;
	}

	@RequestMapping("/addFpShopInformationDz")
	@ResponseBody
	public ResultMap add(HttpServletRequest request, FpShopInformationDzDTO fpShopInformationDz){
		ResultMap result = new ResultMap();

		try{
			logger.info("add fpShopInformationDz  begin .......");

			DTOdateSetUtil.setCreateInfo(fpShopInformationDz);
			DTOdateSetUtil.setUpdateInfo(fpShopInformationDz);

			if(fpShopInformationDz.getUserId()==null || fpShopInformationDz.getInformationId()==null) {
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.ERROR);
			} else {

				FpShopInformationDzDTO fsi = new FpShopInformationDzDTO();
				fsi.setUserId(fpShopInformationDz.getUserId());
				fsi.setInformationId(fpShopInformationDz.getInformationId());
				List<FpShopInformationDzDTO> list = fpShopInformationDzService.queryList(fsi);
				
				if(list.size() >0) {//取消点赞
					fpShopInformationDzService.delete(list.get(0).getId());
				} else {//点赞
					int id = fpShopInformationDzService.insert(fpShopInformationDz);
					result.setAttribute("id", id);
				}
				
				FpShopInformationDTO fpShopInformation = new FpShopInformationDTO();
				fpShopInformation.setId(fpShopInformationDz.getInformationId());
				fpShopInformation.setUserId(fpShopInformationDz.getUserId());
				List<FpShopInformationDTO> fsilist = fpShopInformationService.queryCurdayInformations(fpShopInformation);

				result.setAttribute("item", fsilist.get(0));
				result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
				result.setStatus(ResultMap.STATUS.SUCCESS);
			}

			logger.info("add fpShopInformationDz list end .......");
		}catch(Exception e){
			log.error("add fpShopInformationDz error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}

		return result;
	}


	@RequestMapping("/detailFpShopInformationDz")
	@ResponseBody
	public ResultMap detail(HttpServletRequest request, FpShopInformationDzDTO fpShopInformationDz){
		ResultMap result = new ResultMap();

		try{
			logger.info("query detail fpShopInformationDz  begin .......");

			Integer id = fpShopInformationDz.getId();

			if(id==null){
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}

			FpShopInformationDzDTO dto = fpShopInformationDzService.loadById(id);

			result.setAttribute("dto",dto);

			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);

			logger.info("query detail fpShopInformationDz  end .......");
		}catch(Exception e){
			log.error("query detail fpShopInformationDz error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}

		return result;
	}

	@RequestMapping("/deleteFpShopInformationDz")
	@ResponseBody
	public ResultMap delete(HttpServletRequest request, FpShopInformationDzDTO fpShopInformationDz){
		ResultMap result = new ResultMap();

		try{
			logger.info("delete fpShopInformationDz  begin .......");

			Integer id = fpShopInformationDz.getId();

			if(id==null){
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}

			fpShopInformationDzService.delete(id);

			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);

			logger.info("delete fpShopInformationDz  end .......");
		}catch(Exception e){
			log.error("delete fpShopInformationDz error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}

		return result;
	}

	@RequestMapping("/updateFpShopInformationDz")
	@ResponseBody
	public ResultMap update(HttpServletRequest request, FpShopInformationDzDTO fpShopInformationDz){
		ResultMap result = new ResultMap();

		try{
			logger.info("update fpShopInformationDz  begin .......");

			Integer id = fpShopInformationDz.getId();

			if(id==null){
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}

			DTOdateSetUtil.setUpdateInfo(fpShopInformationDz);

			fpShopInformationDzService.update(fpShopInformationDz);

			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);

			logger.info("update fpShopInformationDz list end .......");
		}catch(Exception e){
			log.error("update fpShopInformationDz error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}

		return result;
	}

	@RequestMapping("/updateForceFpShopInformationDz")
	@ResponseBody
	public ResultMap updateForce(HttpServletRequest request, FpShopInformationDzDTO fpShopInformationDz){
		ResultMap result = new ResultMap();

		try{
			logger.info("updateForce fpShopInformationDz  begin .......");

			Integer id = fpShopInformationDz.getId();

			if(id==null){
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}

			DTOdateSetUtil.setUpdateInfo(fpShopInformationDz);

			fpShopInformationDzService.updateForce(fpShopInformationDz);


			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);

			logger.info("updateForce fpShopInformationDz list end .......");
		}catch(Exception e){
			log.error("updateForce fpShopInformationDz error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}

		return result;
	}

}
